<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入文章标题" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="发表时间" prop="created">
        <el-date-picker clearable v-model="queryParams.created" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择发表时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间" prop="modified">
        <el-date-picker clearable v-model="queryParams.modified" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="文章分类" prop="categories">
        <el-select v-model="queryParams.categories" placeholder="请选择文章分类" clearable>
          <el-option v-for="dict in category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="文章标签" prop="tags">
        <el-input v-model="queryParams.tags" placeholder="请输入文章标签" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="是否允许评论" prop="allowComment">
        <el-select v-model="queryParams.allowComment" placeholder="请选择是否允许评论" clearable>
          <el-option v-for="dict in comment_switch" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['show:article:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['show:article:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['show:article:remove']">删除</el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="50px" />
      <el-table-column label="文章标题" align="center" prop="title" />
      <!-- <el-table-column label="文章具体内容" align="center" prop="content">
        <template #default="scope">
          <span>{{ scope.row.content.slice(0, 60) }}</span>...
        </template>
</el-table-column> -->
      <el-table-column label="点击量" align="center" prop="statistic.hits" />
      <el-table-column label="评论总数" align="center" prop="statistic.commentsNum" />
      <el-table-column label="文章分类" align="center" prop="categories">
        <template #default="scope">
          <dict-tag :options="category" :value="scope.row.categories" />
        </template>
      </el-table-column>
      <el-table-column label="文章缩略图" align="center" prop="thumbnail" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.thumbnail" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['show:article:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['show:article:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改文章操作对话框,在修改的时候,可以对评论执行删除操作 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="articleRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="文章具体内容">
          <editor :key="form.content == null" v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="文章分类" prop="categories">
          <el-radio-group v-model="form.categories">
            <el-radio v-for="dict in category" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文章标签" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入文章标签" />
        </el-form-item>
        <el-form-item label="是否允许评论" prop="allowComment">
          <el-radio-group v-model="form.allowComment">
            <el-radio v-for="dict in comment_switch" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文章缩略图" prop="thumbnail">
          <image-upload v-model="form.thumbnail" />
        </el-form-item>
        <!-- 对评论进行展示 -->
        <label style="font-size:24px;margin-left: 50px;">评论</label>
        <el-table :data="form.comments" style="width: 100%; margin-top: 20px;">
          <el-table-column label="评论内容" align="left" width="500px">
            <template #default="scope">
              <span>{{ scope.row.content }}</span>
            </template>
          </el-table-column>
          <el-table-column label="评论作者" align="center" prop="author" />
          <el-table-column label="评论时间" align="center" prop="created">
            <template #default="scope">
              <span>{{ parseTime(scope.row.created, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="danger" icon="Delete" @click="handleDeleteComment(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Article">
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from "@/api/show/article";
import { delComment } from "@/api/show/comment";
const { proxy } = getCurrentInstance();
const { comment_switch, category } = proxy.useDict('comment_switch', 'category');

const articleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    content: null,
    created: null,
    modified: null,
    categories: null,
    tags: null,
    allowComment: null,
  },
  rules: {
    title: [
      { required: true, message: "文章标题不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "文章具体内容不能为空", trigger: "blur" }
    ],
    created: [
      { required: true, message: "发表时间不能为空", trigger: "blur" }
    ],
    modified: [
      { required: true, message: "修改时间不能为空", trigger: "blur" }
    ],
    allowComment: [
      { required: true, message: "是否允许评论不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询文章操作列表 */
function getList() {
  loading.value = true;
  listArticle(queryParams.value).then(response => {
    articleList.value = response.list;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    created: null,
    modified: null,
    categories: null,
    tags: null,
    allowComment: null,
    thumbnail: null,
    statistic: {
      hits: null,
      commentsNum: null
    },
    comments: []
  };
  proxy.resetForm("articleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  form.value.content = null;
  open.value = true;
  title.value = "添加文章操作";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getArticle(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改文章操作";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["articleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateArticle(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArticle(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      } reset();

    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除文章操作编号为"' + _ids + '"的数据项？').then(function () {
    return delArticle(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}


/**删除评论按钮操作,执行删除操作之后重新获取数据 */
function handleDeleteComment(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除评论编号为"' + _ids + '"的数据项？').then(function () {
    return delComment(_ids);
  }).then(() => {
    getArticle(row.articleId).then(response => {
      form.value = response.data;
    })
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}
getList();
</script>
